#include "networktools.h"

#include <stdio.h>

NetworkTools::NetworkTools(Settings* ptrSettings)
{
	_ptrSettings = ptrSettings;
	http = new Http();
}

NetworkTools::~NetworkTools()
{

}

bool NetworkTools::Connect()
{
	int retval = net_init ();
	if (retval < 0) {
		return false;
	}
	else
	{
		return true;
	}
}

//return true if found
bool NetworkTools::CheckPortiiUpdate(int *version)
{
  static char buf[128];
  static u8 *outbuf;
  u32 outlen;

  int retval;
  u32 http_status;

  snprintf(buf, 128, "http://www.portii.com/current_release/version.txt");

  retval = http->http_request(buf, 1 << 31);
  
  if (!retval) {
    return false;
  }

  retval = http->http_get_result(&http_status, &outbuf, &outlen);
  if (!retval) return false;

  if(atoi((char*)outbuf) > *(version))
  {
	 *(version) = atoi((char*)outbuf);
     return true;
  }
  
  return false;

}

bool NetworkTools::GetFile(std::string filename, u8 **data, u32* size)
{
  int retval;
  u32 http_status;
  
  retval = http->http_request(filename.c_str(), 1 << 31);
  
  if (!retval) {
    return false;
  }

  retval = http->http_get_result(&http_status, data, size); 
  if (!retval) return false;
  
  return true;
}

//return true on success
bool NetworkTools::UpdatePortii()
{
    static u8 *outbuf;
    u32 outlen;

    //TODO Get all parts of file if needed

	if(GetFile("http://www.portii.elf/current_release/boot.elf", &outbuf, &outlen))
	{
		//SaveFile
	}
	else
		return false;
		
	if(GetFile("http://www.portii.elf/current_release/meta.xml", &outbuf, &outlen))
	{
		//SaveFile
	}
	else
		return false;
	
	if(GetFile("http://www.portii.elf/current_release/icon.png", &outbuf, &outlen))
	{
		//SaveFile
	}
	else
		return false;
		
	
	/*
	load_network();
	s32 main_server = connect_to_server((char*)_ptrSettings->GetPortiiIp());
	
	FILE* f = fopen(get_location((char*)"boot.elf", (char*)"/apps/portii"), "wb");
	sprintf(buffer, "%s/boot.elf", _ptrSettings->GetPortiiNetworkPath());
	send_message(main_server, buffer, (char*)_ptrSettings->GetPortiiHostName());
	instructions_update();
	get_file(main_server, f);
	fclose(f);
	
	f = fopen(get_location((char*)"meta.xml", (char*)"/apps/portii"), "wb");
	sprintf(buffer, "%s/meta.xml", _ptrSettings->GetPortiiNetworkPath());
	send_message(main_server, buffer, (char*)_ptrSettings->GetPortiiHostName());
	instructions_update();
	get_file(main_server, f);
	fclose(f);
	
	f = fopen(get_location((char*)"icon.png", (char*)"/apps/portii"), "wb");
	sprintf(buffer, "%s/icon.png", _ptrSettings->GetPortiiNetworkPath());
	send_message(main_server, buffer, (char*)_ptrSettings->GetPortiiHostName());
	instructions_update();
	get_file(main_server, f);
	fclose(f);
		
	net_close(main_server);
	*/
	
	return true;
	
}

//return list of levels on the network for download
std::vector<std::string> NetworkTools::GetNetworkLevels()
{
	
}


//download network level, return true on success
bool NetworkTools::DownloadNetworkLevel(std::string lvlname)
{
    static char buf[128];
	
    static u8 *outbuf;
    u32 outlen;
	
    snprintf(buf, 128, "http://www.portii.com/levels/%s", lvlname.c_str());
	if(GetFile(buf, &outbuf, &outlen))
	{
		//SaveFile
	}
	else
		return false;
	
	return true;
}


//return true on success
bool NetworkTools::PostLevelToNetwork(std::string filename)
{

}
